/*
 * @Author: your name
 * @Date: 2021-11-04 15:33:00
 * @LastEditTime: 2022-02-16 11:25:48
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue-antd-admin-new\src\utils\uploading.js
 */



import {  Message } from 'element-ui'

const fileKey = 'file'

/**
 * @description: 
 * @param {*} file
 * @param {*} type
 * @param {*} way    excel时传表名 传空为正常上传
 * @param {*} path   路径存放 没有/开头
 * @param {*} size   路径存放 没有/开头
 * @return {*}
 */
export let  uploading = function(file, type, way = '',path = '',size = 2) {
  var formData = new FormData();
  if (checkType( file.type.toLowerCase() , type) && checksize(file.size, size)) {
    formData.append(fileKey, file);
    formData.append('fileType', type);
    if (way) { 
      formData.append('way', way)
    }
    if(path){
      formData.append('path', path)
    }
    return formData
  } else {
    return false
  }
}


function checkType(fileType, type) {
  switch (type) {

    case 'excel':
      if (fileType == "application/vnd.ms-excel" || fileType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
        return true
      } else {
        Message.error("文件类型只能是xls,xlsx");
      }
      return false;
     case 'images':
      if (fileType == "image/png" || fileType == "image/jpg" || fileType == "image/gif" || fileType == "image/jpeg") {
        return true
      } else {
        Message.error("文件类型只能是png,jpg,gif,jpeg");
      }

      case 'possess':
      return true



  }
}


function checksize(size, maxSizeMb) {
  if (size > maxSizeMb * 1024 * 1024) {
    Message.error(`文件不能超过${maxSizeMb}MB`);
    return false
  } else {
    return true
  }
}
// export default {

//  export default {
//   install: function(Vue) {

//     Vue.prototype.uploading = (file, type, way = '',path = '') => uploading(file, type, way ,path )
//   }
 

// }